home *** CD-ROM | disk | FTP | other *** search
- Path: cortex.corpus.uni-muenster.de!news
- From: gutschk@uni-muenster.de (Markus Gutschke)
- Newsgroups: comp.lang.c,comp.windows.x.motif
- Subject: Re: [Q:] flex input from string?
- Date: 14 Jan 1996 13:19:08 GMT
- Organization: Markus Gutschke, Schlage 5a, 48268 Greven-Gimbte, Germany
- Distribution: inet
- Message-ID: <GUTSCHK.96Jan14141909@cortex.corpus.uni-muenster.de>
- References: <30F660A4.41C6@vmars.tuwien.ac.at>
- NNTP-Posting-Host: pppe085.uni-muenster.de
- In-reply-to: Joachim Fabini's message of Fri, 12 Jan 1996 14:22:44 +0100
-
- In article <30F660A4.41C6@vmars.tuwien.ac.at> Joachim Fabini <jo@vmars.tuwien.ac.at> writes:
- > My question: (f)lex reads by default from the FILE* yyin,
- > defaulting to stdin. How can I read input from a _STRING_ ? Say, I get
- > the text field's value and pass it as input to the scanner, that
- > returns some token and I know exactly if the input was correct or not.
-
- I would advise you to get hold of a copy of
-
- John R. Levine, Tony Mason, Doug Brown; Lex&Yacc; UNIX
- Programming Tools; O'Reilly&Associates, Inc; Second Edition;
- October 1992; ISBN 1-56592-000-7.
-
- This book gives an excellent overview on Lex and Yacc related problems
- and summarizes differences between commonly available implementations.
-
- It says the following on "Input from Strings" pp. 156:
-
- > INPUT FROM STRINGS
- >
- > Normally lex reads from a file, but sometimes you want it to read from
- > some other source, such as a string in memory. All versions of lex
- > make this possible, but the details vary considerably.
- >
- > ...
- >
- > FLEX
- >
- > Although flex provides an input() function, it gets characters using
- > optimized in-line code. You can redefine YY_INPUT, the macro it uses
- > to read blocks of data. It is called as:
- >
- > YY_INPUT(buffer, result, max_size)
- >
- > where buffer is a character buffer, result is a variable in which to
- > store the number of characters actually read, and max_size is the size
- > of the buffer. To read from a string, have your version of YY_INPUT
- > copy data from your string buffer.
- >
- > %{
- > #undef YY_INPUT
- > #define YY_INPUT(b, r, ms) (r = my_yyinput(b, ms))
- > %}
- > ...
- > extern char myinput[];
- > extern char *myinputptr; /* current position in myinput */
- > extern int myinputlim; /* end of data */
- >
- > int my_yyinput(char *buf, int max_size)
- > {
- > int n = min(max_size, myinputlim - myinputptr);
- >
- > if (n > 0) {
- > memcpy(buf, myinputptr, n);
- > myinputptr += n;
- > }
- > return n;
- > }
-
- So, as long as you stick to using Flex, it appears that redefining
- YY_INPUT will do the trick. Nonetheless, if you want to release your
- code under a license other than GPL, you should be aware of copyright
- restriction imposed onto Flex. Thus, you might have to get hold of a
- different lexical scanner generator; in this case you should have a
- look in "Lex&Yacc" on how you need to modify your code...
-
-
- Markus
- --
- Markus Gutschke Internet: gutschk@math.uni-muenster.de
- Schlage 5a
- D-48268 Greven-Gimbte
- Germany
-